package cn.com.wxd.common;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Title:XmlUtil.java
 * Description:cn.com.wxd.util
 * <p>
 * Company: Amumu管理平台
 * Copyright: Copyright (c) 2016
 * All right reserved.
 * Created time: 2017-4-9 下午10:57:12
 *
 * @author ComXu
 * @version 2.0
 */
public class XmlUtil {
    private static final Logger log = LoggerFactory.getLogger(XmlUtil.class);

    /**
     * 将对象直接转换成String类型的 XML输出
     *
     * @param obj
     *
     * @return
     */
    public static String convertToXml(Object obj) {
        // 创建输出流  
        StringWriter sw = new StringWriter();
        try {
            // 利用jdk中自带的转换类实现  
            JAXBContext context = JAXBContext.newInstance(obj.getClass());

            Marshaller marshaller = context.createMarshaller();
            // 格式化xml输出的格式  
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.FALSE);
            // 将对象转换成输出流形式的xml  
            marshaller.marshal(obj, sw);
        } catch (JAXBException e) {
            log.error("将对象转成xml字符串错误", e);
        }
        return sw.toString();
    }

    /**
     * 将对象根据路径转换成xml文件
     *
     * @param obj
     * @param path
     *
     * @return
     */
    public static void convertToXml(Object obj, String path) {
        try {
            // 利用jdk中自带的转换类实现  
            JAXBContext context = JAXBContext.newInstance(obj.getClass());

            Marshaller marshaller = context.createMarshaller();
            // 格式化xml输出的格式  
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.FALSE);
            // 将对象转换成输出流形式的xml  
            // 创建输出流  
            FileWriter fw = null;
            try {
                fw = new FileWriter(path);
            } catch (IOException e) {
                log.error("将xml字符串输出到文件错误。", e);
            }
            marshaller.marshal(obj, fw);
        } catch (JAXBException e) {
            log.error("将对象转成xml文件", e);
        }
    }

    /**
     * 将String类型的xml转换成对象
     */
    @SuppressWarnings("rawtypes")
    public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
        Object xmlObject = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            // 进行将Xml转成对象的核心接口  
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader sr = new StringReader(xmlStr);
            xmlObject = unmarshaller.unmarshal(sr);
        } catch (JAXBException e) {
            log.error("将xml转换成Object出现错误", e);
        }
        return xmlObject;
    }

    /**
     * 将file类型的xml转换成对象
     */
    @SuppressWarnings("rawtypes")
    public static Object convertXmlFileToObject(Class clazz, String xmlPath) {
        Object xmlObject = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            FileReader fr = null;
            try {
                fr = new FileReader(xmlPath);
            } catch (FileNotFoundException e) {
                log.error("读取xml文件错误", e);
            }
            xmlObject = unmarshaller.unmarshal(fr);
        } catch (JAXBException e) {
            log.error("将xml文件转换成Object错误", e);
        }
        return xmlObject;
    }

    /**
     * 将对象直接转换成String类型的 XML输出
     *
     * @param obj
     *
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String listconvertToXml(List list, Class clazz) {
        JaxbList smsj = new JaxbList(list);
        // 创建输出流  
        StringWriter sw = new StringWriter();
        try {
            // 利用jdk中自带的转换类实现  
            JAXBContext context = JAXBContext.newInstance(clazz, smsj.getClass());

            Marshaller marshaller = context.createMarshaller();
            // 格式化xml输出的格式  
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.FALSE);
            // 将对象转换成输出流形式的xml  
            marshaller.marshal(smsj, sw);
        } catch (JAXBException e) {
            log.error("将List转成xml字符串错误", e);
        }
        return sw.toString();
    }

    /**
     * 将xml对象转化成相应的List集合
     *
     * @param obj
     *
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static List xmlconvertToList(String xmlStr, Class clazz) {
        JaxbList jaxbList = new JaxbList();
        try {
            JAXBContext context = JAXBContext.newInstance(clazz, JaxbList.class);
            // 进行将Xml转成对象的核心接口
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader sr = new StringReader(xmlStr);
            jaxbList = (JaxbList) unmarshaller.unmarshal(sr);
        } catch (JAXBException e) {
            log.error("将xml转换成List出现错误", e);
        }
        return jaxbList.getItems();
    }

    /**
     * Title:该类分装了List类，该类的List可以使用JaxbList转化为xml或用xml转化为List集合
     * Description:cn.com.wxd.util
     * <p>
     * Company: wxd网站
     * Copyright: Copyright (c) 2016
     * All right reserved.
     * Created time: 2017-4-9 下午11:38:41
     *
     * @author ComXu
     * @version 2.0
     */
    @XmlRootElement(name = "items")
    @XmlAccessorType(XmlAccessType.FIELD)
    static class JaxbList {
        @XmlElement(name = "item")
        private List items;

        public JaxbList() {
            items = new ArrayList();
        }

        public JaxbList(List items) {
            this.items = items;
        }

        public void setItems(List items) {
            this.items = items;
        }

        public List getItems() {
            return items;
        }
    }
}  
